<?
/**
 * VoiceOne Settings/Tech/
 *
 * Frontend to provide access to the tech system in voiceone.
 * @package voiceone
 * @subpackage  settings_tech
 */
  session_start();
  include ("../../admin/config/cfg.php");
  global $cwlang, $relBasePath, $cwLanguage;
  
  //================ PAGE LOGIC ================
  $alert=getAlert();

  if ($_GET["cmd"]=="del") {
    $ret=$asterisk->delChannel($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdn"]["alertDelGood"]."</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdn"]["alertDelBad"]."</div>";
    header("location: $PHP_SELF");die;
  }

  if ($_GET["cmd"]=="up") {
    $ret=$asterisk->moveTrunk($_GET["id"],1);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdngrp"][""]."Trunk Moved</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdngrp"][""]."Trunk not moved</div>";
    $scriptFade="<script>Fat.fade_element(\"row".$_GET["id"]."\", 30, 500, \"#FFFF33\", \"#FFFFFF\");</script>";
  }
  
  if ($_GET["cmd"]=="down") {
    $ret=$asterisk->moveTrunk($_GET["id"],2);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdngrp"][""]."Trunk moved</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdngrp"][""]."Trunk not moved</div>";
    $scriptFade="<script>Fat.fade_element(\"row".$_GET["id"]."\", 30, 500, \"#FFFF33\", \"#FFFFFF\");</script>";
  }
  
  
  // get the technologies list

  $tech=$asterisk->getTechnology(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($tech)) {
    foreach($tech as $t) {
      $id=$t["id"];
      $name=$t["name"];
      $voip=$t["voip"];
      $optionTech.="<option value=\"$id\">$name</option>\n";
    }
  }  

  // create the web interface

  $count=1;
  $ret=$asterisk->getTrunk(0,0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($ret)) {
    foreach($ret as $v) {
      $id=$v["id"];
      $name=$v["name"];
      $speeddial=$v["speeddial"];
      $technology=$v["technology"];
      $key=$v["key"];
      foreach ($tech as $t) if ($t["id"]==$technology) $techName=$t["name"];
      $key=$v["key"];

      //create link
      $entityArr=$asterisk->getEntity($key);
      $entityType=$entityArr[0]["type"];
      error_log($entityType);
      if ($entityType==MISDN_GROUP) $link="misdn/edit/?id=$key";
      else if ($entityType==ZAPATA_GROUP) $link="zapata/edit/?id=$key";
      else if ($entityType==PROVIDER) $link="../providers/edit/?id=$key";
      else if ($entityType==OFFICE) $link="../offices/edit/?id=$key";
      else $link="edit/?id=$id";
      
      //del button
      if ($key==0) $del="<a href=\"$PHP_SELF?cmd=del&id=$id\" onclick=\"if (confirm('Delete trunk $name?')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a>\n";
      else $del="&nbsp;";
      
      //buttons Up and Down      
      if ($count==1) $button="<td>&nbsp;</td>";
      else $button="<td><a href=\"?cmd=up&id=$id\"><img src=\"$relBasePath/public/img/icon_up.gif\" alt=\"up\" /></a></td>";
      if ($count==count($ret)) $button.="<td>&nbsp;</td>";
      else $button.="<td><a href=\"?cmd=down&id=$id\"><img src=\"$relBasePath/public/img/icon_down.gif\" alt=\"down\" /></a></td>";
      
      
      $html.="<tr id=\"row$id\" class=\"firstofgroup rule\"><td class=\"green\">$id</td>$button<td><strong>$speeddial*</strong></td><td>$techName</td><td ><strong><a href=\"$link\">$name</a></strong></td><td>trunk_$id</td><td>$del</td></tr>\n";
      $count++;
    }
  }    
  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=$cwlang["isdn"][""]?>Available Trunk</h1>
    <h3><a href="edit/" class="admin" ><?=$cwlang["isdngrp"][""]?>Add a new custom trunk</a></h3>
  </div>
  <p class="blockintro">Ready to use trunk. The entries below list the available trunks you can use inside VoiceOne.</p>
  <div class="block">
  <table class="todolist" cellspacing="0" >
    <tr>
      <th>Id</th><th>&nbsp;</th><th>&nbsp;</th><th>Speed Dial</th><th>Technology</th><th>Trunk name</th><th>Context</th><th>&nbsp;</th>
    </tr>
    <?=$html?>
  </table>
  </div>
  
  <?
  echo $scriptFade;
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <?$sel="trunk";include("sidebar.php");?>
          <h1><?=$cwlang["isdn"][""]?>Custom Trunk</h1>
            <?=$cwlang["isdn"][""]?>
            Sign here your custom trunk, so you can use outside resources.
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================

  $smarty->assign("section", "tech");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

